package cn.fan.service;

import java.util.List;

import cn.fan.model.LastLoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.geo.*;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;



@Service
public class PlaceManager {

	@Autowired
	private MongoTemplate mongoTemplate;


	/**
	 * 功能描述:根据经纬度参数查询当前位置的附近范围内是否有司机
	 * 
	 * @param lon    经度
	 * @param lat    纬度
	 * @param radius 半径,单位为m
	 * @return
	 */
	public void getDrivers(Double lon, Double lat, Long radius) {
	 	//radius 单位为m，所以要转成km.
		NearQuery query = NearQuery.near(new Point(lon, lat)).maxDistance(new Distance(radius / 1000, Metrics.KILOMETERS))
				.query(new Query(Criteria.where("entityType").is("driver")));			
         //tpl.geoNear()方法是专门查询地图附近集合的集成mongo的方法
		GeoResults<LastLoc> geoResults = mongoTemplate.geoNear(query, LastLoc.class);
		if (geoResults != null) {
			List<GeoResult<LastLoc>> content = geoResults.getContent();
			//如果想获取查询结果集是自己封装的数据。只需要在进行一次遍历
			// for (GeoResult<SchoolAddressInfo> geoResult: content) {
			//    SchoolAddressInfo schoolAddressInfo = geoResult.getContent();
			//}

			if (content != null && content.size() > 0) {

			} else {

			}
		} else {

		}

	}

}

